package org.jeecg.modules.flow.callback.move;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 移库单关闭 回调Bean
 *
 * @author lumingyi
 * @version 1.0.0
 * @since 2023-11-16
 */
@RequiredArgsConstructor
@Component
public class MoveReceiptCloseBean<T> extends BaseCustomBeanRun<T> {

    @Autowired
    private IWmsMoveReceiptService moveReceiptService;

    /**
     * 业务逻辑 移库单关闭
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        String taskId = baseFlowDto.getInfoId();
        WmsMoveReceipt task = moveReceiptService.getById(taskId);
        if (ObjectUtil.isEmpty(task)) {
            throw new JeecgBootException("移库单不存在!");
        }
        if (task.getDocumentStatus().equals(DocumentStatusEnum.COMPLETE.getCode())) {
            throw new JeecgBootException("该移库单不可关闭!");
        }
        moveReceiptService.update(new LambdaUpdateWrapper<WmsMoveReceipt>()
                .eq(WmsMoveReceipt::getId, taskId)
                .set(WmsMoveReceipt::getDocumentStatus, DocumentStatusEnum.CLOSE.getCode()));
        // 当前前端调用业务需要你返回的
        baseFlowDto.setResult(Result.OK());
        // id是保存业务记录的id。用于生成业务流程信息记录
        baseFlowDto.setInfoId(taskId);
    }
}
